/*
 * UMLSynch plugin for Eclipse-CDT
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package com.googlecode.umlsynch.eclipse.cdt;

import com.googlecode.umlsynch.eclipse.UMLSynchPlugin;
import com.googlecode.umlsynch.eclipse.astutils.ASTUtils;
import com.googlecode.umlsynch.rpc.IEditorRpc;
import com.googlecode.umlsynch.rpc.TClassDescriptor;
import com.googlecode.umlsynch.rpc.TClassOperation;
import com.googlecode.umlsynch.rpc.TFileDescriptor;
import com.googlecode.umlsynch.rpc.base.IDebug;


public class TCdtEditorRpc implements IEditorRpc {

	//private IDebug debug = UMLSynchPlugin.getDebug();
	private ASTUtils astutils = ASTUtils.instance();

	public TCdtEditorRpc() {		
	}

	@Override
	public void OpenClass(TClassDescriptor desc) {
//		  debug.Note("OpenClass");

		  TClassDescriptor sdesc= astutils.IdentifyClass(desc);
		  if (sdesc != null) {
			  astutils.GetClassFile(sdesc);	
		  } else {
			  return;
		  }

		  // UMLSynchPlugin.getDefault().getEditor().OpenFile(file, line);		  
		  
		  /*
		  DefinitionView inst = DefinitionView.GetInstance();
		  if (inst != null) {
			  debug.Note("Not null instance");
			  inst.OpenFile(sdesc.file.project, sdesc.file.path, 20);
		  } else {
			  debug.Note("Not instance");
		  }
		  */
	}

	@Override
	public void OpenFile(TFileDescriptor desc, Integer line) {
		// Do nothing, method implemented in the UMLSynchPlugin core

	}

	@Override
	public void OpenMethodDeclaration(TClassOperation desc) {
		// Interface issue:
		// Class undefined
	}

	@Override
	public void OpenMethodImplementation(TClassOperation desc) {
		// Interface issue:
		// Class undefined 
/*		
		if ((className.className != null)
				&& (methodName != null)) {
			debug.Debug("all not null");
			if (Desc == null)			
				astutils.OpenMethodImplementation(className, methodName, true);
			else if (Desc.equalsIgnoreCase("OpenDeclarationView")) {
				debug.Debug("OpenDeclarationView");
				DefinitionView inst = DefinitionView.GetInstance();
				if (inst != null) {
					debug.Debug("1");
					IASTFileLocation location = astutils.OpenMethodImplementation(className, methodName, false);
					
					if (location != null) {
						String fname = location.getFileName();
					    
						int start = location.getEndingLineNumber();
						int pos = fname.indexOf(className.projectName) + className.projectName.length() + 1;
						
						debug.Debug("Found:" + fname + " pos:" + start
								+ " node:" + location.getNodeLength());
						
						fname = fname.substring(pos, fname.length());
					
						inst.OpenFile(className.projectName, fname, start);
					} else {
						debug.Error("Method " + methodName + " location is null");
					}
				}
			} else if (Desc.equalsIgnoreCase("ProjectExplorer")) {
				// Focus in project explorer
			}
		}		
		*/
	}

}
